package com.fulihui.wgj.admin.api.region;

import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.region.request.RegionQueryProvinceRequest;
import com.fulihui.wgj.admin.api.region.vo.WxRegionVO;
import com.fulihui.wgj.admin.integration.WxRegionServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-24 17:13
 */
@RestController
@RequestMapping("/region")
@Api(tags = "微信国家地区选择接口")
public class RegionManageController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(RegionManageController.class);

    @Autowired
    private WxRegionServiceClient wxRegionServiceClient;

    @PostMapping("/getCountry")
    @ApiOperation("获取所有国家")
    public Result<List<WxRegionVO>> queryCountry() {
        Result<List<WxRegionVO>> listResult = wxRegionServiceClient.queryCountry();
        return listResult;
    }

    @PostMapping("/getProvince")
    @ApiOperation("根据国家获取省份")
    public Result<List<WxRegionVO>> queryProvince(@RequestBody @Valid RegionQueryProvinceRequest request,
                                                  BindingResult validResult) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }
        Result<List<WxRegionVO>> listResult = wxRegionServiceClient.queryProvince(request);
        return listResult;
    }
}
